<template>
  <div>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>添加商品</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图区 -->
        <el-card>
            <!-- 提示区 -->
            <el-alert title="添加商品信息" type="info" center show-icon :closable="false"></el-alert>
            <!-- 进度条 -->
            <el-steps :space="200" :active="activeIndex - 0" finish-status="success" align-center>
                <el-step title="基本信息"></el-step>
                <el-step title="商品参数"></el-step>
                <el-step title="商品图片"></el-step>
                <el-step title="商品内容"></el-step>
                <el-step title="完成"></el-step>
            </el-steps>
            <!-- tab栏 -->
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px" label-position="top">
                <el-tabs v-model="activeIndex" :tab-position="'left'">
                    <el-tab-pane label="基本信息" name="0">
                        <el-form-item label="商品名称" prop="goodsName">
                            <el-input v-model="addForm.goodsName"></el-input>
                        </el-form-item>
                        <el-form-item label="商品价格" prop="goodsPrice">
                            <el-input v-model="addForm.goodsPrice" type="number"></el-input>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane label="商品参数" name="1">
                        <!-- 渲染表单item项 -->
                        <el-form-item label="标签">
                            <!-- 复选框组bug -->
                            <el-tag v-for="item in tagList" :key="item.tagId" @click="getTag(item)">{{item.tagName}}</el-tag>
                        </el-form-item>
                        <el-form-item label="已选标签">
                            <!-- 复选框组bug -->
                            <el-tag v-for="item in selectedTags" :key="item.tagId" @close="deleleTag(item)" :closable='true' >{{item.tagName}}</el-tag>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane label="商品图片" name="2">
                        <el-divider content-position="left">上传商品封面图片</el-divider>
                        <!-- action图片上传地址 -->
                        <el-upload
                            name="coverImg"
                            :action="uploadURL"
                            :on-preview="handlePreview"
                            :on-remove="handleRemove"
                            list-type="picture"
                            :headers="headerObj"
                            :on-success="handleSuccess"
                            :limit= 1
                            >
                            <el-button size="small" type="primary">点击上传</el-button>
                            <div slot="tip" class="el-upload__tip">只能上传一张封面图，且只能上传jpg/png文件，且不超过500kb</div>
                        </el-upload>
                        <!-- action图片上传地址 -->
                        <el-divider content-position="left">上传商品展示图片</el-divider>
                        <el-upload
                            name="carousel"
                            :action="uploadCarouselURL"
                            :on-preview="handleCarouselPreview"
                            :on-remove="handleCarouselRemove"
                            list-type="picture"
                            :headers="headerObj"
                            :on-success="handleCarouselSuccess"
                            >
                            <el-button size="small" type="primary">点击上传</el-button>
                            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                        </el-upload>
                    </el-tab-pane>
                    <el-tab-pane label="商品内容" name="3">
                        <!-- 富文本编辑器组件 -->
                        <quill-editor v-model="addForm.goodsIntro"/>
                        <!-- 添加商品按钮 -->
                        <el-button class="btnAdd" type="primary" @click="add">添加商品</el-button>
                    </el-tab-pane>
                </el-tabs>
            </el-form>
            <!-- 图片预览 -->
            <el-dialog
            title="图片预览"
            :visible.sync="previewVisible"
            width="50%">
                <img :src="previewPath" class="perviewImg">
            </el-dialog>
        </el-card>
  </div>
</template>

<script>
export default {
    data() {
        return {
            activeIndex: '0',
            //添加商品的表单验证对象
            addForm: {
                goodsName: '',
                goodsIntro: '',
                goodsPrice: 0,
                goodsCoverImg: '',
                tags: [],
                carousels: []
            },
            addFormRules: {
                goodsName: [
                    { required: true, message: '请输入商品名称', trigger: 'blur'}
                ],
                goodsPrice: [
                    { required: true, message: '请输入商品价格', trigger: 'blur'}
                ]
            },
            //动态参数列表
            tagList: [],
            //已选tags
            selectedTags: [],
            uploadURL: '/api/goods/uploadCoverImg/coverImg',
            uploadCarouselURL: '/api/goods/uploadCarousel/carousel',
            headerObj: {
                Authorization: window.sessionStorage.getItem('token')
            },
            previewPath: '',
            previewVisible: false
        }
    },
    created() {
        this.getTagList()
    },
    methods: {
        async getTagList() {
            const {data: res} = await this.$http.get('/api/goods/getAllTagForInsert')
                if (res.code !== 200) {
                    return this.$message.error('获取标签列表失败')
                }
                this.tagList = res.data
        },
        getTag(tag) {
            if (this.selectedTags.indexOf(tag) == '-1') {
                this.selectedTags.push(tag)
            } else {
                this.$message.error('无法添加重复的标签')
            }
            console.log(this.selectedTags.indexOf(tag))
        },
        deleleTag(tag) {
            const index = this.selectedTags.indexOf(tag)
            this.selectedTags.splice(index,1)
        },
        //处理封面图片预览效果
        handlePreview(file) {
            this.previewPath = file.response.data
            this.previewVisible = true
        },
        //处理移除封面图片效果
        handleRemove(file) {
            console.log(file)
            //置空属性
            this,addForm.goodsCoverImg= ''
        },
        //监听封面图片上传成功的event
        handleSuccess(response) {
            //得到图片信息对象
            console.log(response)
            this.addForm.goodsCoverImg = response.data
            console.log(this.addForm)
        },
        //处理car图片预览效果
        handleCarouselPreview(file) {
            this.previewPath = file.response.data
            this.previewVisible = true
        },
        //处理移除car图片效果
        handleCarouselRemove(file) {
            const filePath = file.response.data
            const i = this.addForm.carousels.findIndex(x => x.url === filePath)
            this.addForm.carousels.splice(i,1)
            console.log(this.addForm)
        },
        //监听car图片上传成功的event
        handleCarouselSuccess(response) {
            //得到图片信息对象
            const carousel = {
                url: response.data
            }
            this.addForm.carousels.push(carousel)
            console.log(this.addForm)
        },
        add() {
            this.$refs.addFormRef.validate( async valid => {
                if (!valid) {
                    return this.$message.error('请填写必要的表单项目')
                }
                this.addForm.tags = this.selectedTags
                //提交表单
                const {data: res} = await this.$http.put('/api/goods/addGoods', this.addForm)
                if (res.code !== 200) {
                    return this.$message.error('商品添加失败')
                }
                this.$message.success('商品添加成功')
                this.$router.push('/goods')
            })
        }

    }
}
</script>

<style lang="less" scoped>
.el-tag {
    margin-left: 20px;
}
.perviewImg {
    width: 100%;
}
.btnAdd {
    margin-top: 15px;
}
</style>